perf(schema): compile compose-spec schema once#890
Merged
glours merged 1 commit intoJun 29, 2026
Conversation
Validate rebuilt a jsonschema Compiler and recompiled the embedded compose-spec schema (including loading and resolving the draft 2020-12 meta-schema) on every call. Compile it once behind a sync.Once instead, which keeps Validate cheap on the loader hot path and makes it safe to call concurrently without sharing any mutable compiler state. Switch MustCompile to Compile so a schema compilation failure is returned as an error instead of panicking. Add a concurrent Validate test (run with -race) as a regression guard. Relates to docker/compose#13866 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
b1a035a to
2133283
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
schema.Validaterebuilt ajsonschema.Compilerand recompiled the embeddedcompose-spec.jsonschema — including loading and resolving the draft 2020-12 meta-schema — on every call. This commit compiles the schema once behind async.Onceand reuses the resulting*jsonschema.Schema.Also switches
MustCompile→Compileso a compilation failure is returned as an error instead of panicking, and adds a concurrentValidateregression test (run with-race).Why
The loader calls
Validateonce per file on the hot path, so recompiling the full schema each time is needlessly expensive. Compiling once:Validatecheap (~µs/op instead of a full schema compilation per call);Validatesafe to call concurrently without sharing any mutable compiler state.Context
Investigated while looking at docker/compose#13866 (
fatal error: concurrent map iteration and map writereported inside schema compilation). Note: I could not reproduce that crash throughschema.Validate— even under heavy concurrency with-race, both before and after this change, no data race fires. The current loader is sequential and eachValidatecall already keeps independent state, so the reported concurrency most likely originates in the caller (docker/compose). This change is therefore offered as a performance + hardening improvement rather than a fix for that specific crash; it relates to the issue without claiming to close it.Test
go test -race ./schema/...passes, including the newTestValidateConcurrent(50 goroutines) and an ad-hoc hammer of 4000 concurrent validations of the real schema.🤖 Generated with Claude Code